import mapping from '../java110Mapping.js'
import {
	request
} from '../java110Request.js'
import conf from '../../../conf/config.js'

import {
	refreshOpenId,
	getOpenIdByCode
} from '../api/systemApi.js'

import {
	isNull,
	isNotNull,
	isEmpty
} from '../utils/StringUtil.js';

import {
	getH5Url
} from '../utils/PageUtil.js'

const HAS_USER_OPEN_ID = conf.baseUrl + 'app/userAuth/checkUserHasOpenId';




/**
 * 检查会话是否 存在
 * add by 吴学文 QQ:928255095
 */
export function checkSession() {
	return new Promise((resolve, reject) => {
		let _that = this;
		let loginFlag = wx.getStorageSync(mapping.LOGIN_FLAG);
		let nowDate = new Date();
		console.log('loginFlag && loginFlag.expireTime > nowDate.getTime()', loginFlag, loginFlag.expireTime,
			nowDate.getTime())
		if (loginFlag && loginFlag.expireTime > nowDate.getTime()) {
			console.log('loginFlag && loginFlag.expireTime > nowDate.getTime()', loginFlag.expireTime)
			resolve();
		} else {
			reject();
		}
	});
}

/**
 * 是否登录 跳转鉴权
 * 
 * add by 吴学文 QQ:928255095
 */
export function loginAuth(options, _url) {

	//判断当前登录状态，不调跳转登录界面
	let loginFlag = uni.getStorageSync(mapping.LOGIN_FLAG);
	let nowDate = new Date();
	if (loginFlag && loginFlag.expireTime > nowDate.getTime()) {
		return;
	}
	//待hcCode 不去鉴权
	console.log('options', options)
	if (!isEmpty(options) && options.hasOwnProperty("hcCode")) {
		return;
	}

	let _mallFrom = uni.getStorageSync(mapping.MALL_FROM);
	if(_url){
		uni.setStorageSync('login_to_page',_url);
	}

	// #ifdef H5
	if (_mallFrom && _mallFrom == 'HC_H5') {
		let _systemConf = uni.getStorageSync('java110SystemConfig');

		if (!_systemConf) {
			uni.navigateTo({
				url: '/pages/public/login'
			})
			return;
		}

		let ownerUrl = _systemConf.ownerUrl;

		window.location.href = ownerUrl + "/#/pages/login/showlogin?_returnUrl=" + window.location.href;
		throw new Error('没有登录 请先登录')
	}
	// #endif

	//这里后期 考虑 不是内嵌的情况  
	uni.navigateTo({
		url: '/pages/public/login'
	})
	throw new Error('没有登录 请先登录');
}
/**
 * 是否登录 不跳转 只是判断是否登录
 * 
 * add by 吴学文 QQ:928255095
 */
export function hasLogin() {
	//判断当前登录状态，不调跳转登录界面
	let loginFlag = uni.getStorageSync(mapping.LOGIN_FLAG);
	let nowDate = new Date();
	if (loginFlag && loginFlag.expireTime > nowDate.getTime()) {
		return true;
	}
	return false;
}

/**
 * 刷新openId 功能
 */
export function refreshWechatOpenId(_successUrl, _errorUrl) {
	let _mallFrom = uni.getStorageSync(mapping.MALL_FROM);

	let _userInfo = uni.getStorageSync(mapping.USER_INFO);

	if (mapping.MALL_FROM_APP == _mallFrom) {
		//这里写APP的东西
		return;
	}
	let _isFrame = _mallFrom ? "Y" : "N";

	let _openUrl = '';
	refreshOpenId({
		userId: _userInfo.userId,
		redirectUrl: _successUrl,
		errorUrl: _errorUrl,
		isFrame: _isFrame
	}).then(data => {
		_openUrl = data.openUrl;
		window.location.href = _openUrl;
	}, err => {
		reject(false);
		uni.showToast({
			icon: 'none',
			title: '微信鉴权失败'
		})
	})
}

/**
 * 检查是否有open Id 
 */
export function hasUserOpenId(_rediectUrl) {

	// #ifdef H5
	if (isNull(_rediectUrl)) {
		_rediectUrl = getH5Url();
	}
	//跳转 至鉴权页面
	let redirected = refreshWechatOpenId(_rediectUrl);
	return redirected;
	// #endif

	// #ifndef H5
	wx.login({
		success: function(loginRes) {
			if (loginRes.code) {
				// TODO
				getOpenIdByCode({
					code: loginRes.code,
					appId: 'MALL'
				}).then(_data => {

				})
			}
		},
		fail: function(error) {}
	});
	// #endif
}